<script setup lang="ts">
import { ref } from 'vue'
// 导入统一的帮助文档
import helpContent from '@/docs/help.md?raw'

// 解析 Markdown 内容，根据标题层级构建目录
const parseHelpContent = (content: string) => {
  const sections = content.split('\n## ')
  const categories = []
  
  for (let i = 1; i < sections.length; i++) {
    const section = sections[i]
    const [title, ...content] = section.split('\n')
    const items = []
    
    // 解析三级标题
    const subSections = content.join('\n').split('\n### ')
    for (let j = 1; j < subSections.length; j++) {
      const subSection = subSections[j]
      const [subTitle, ...subContent] = subSection.split('\n')
      items.push({
        title: subTitle,
        content: subContent.join('\n')
      })
    }
    
    categories.push({
      title: title.trim(),
      icon: getIconForCategory(title.trim()),
      items
    })
  }
  
  return categories
}

const getIconForCategory = (title: string) => {
  const iconMap = {
    '快速入门': 'icon-compass',
    '项目管理': 'icon-apps',
    '文档中心': 'icon-book',
    '使用技巧': 'icon-bulb',
    '账号设置': 'icon-user',
    '安全中心': 'icon-safe'
  }
  return iconMap[title] || 'icon-file'
}

const helpCategories = ref(parseHelpContent(helpContent))
const activeCategory = ref(helpCategories.value[0])
</script>

<template>
  <div class="help-container">
    <div class="help-header">
      <h1>帮助中心</h1>
      <p>欢迎使用极研协作平台，这里为您提供全面的使用指南和问题解答</p>
    </div>

    <div class="help-content">
      <a-row :gutter="16">
        <a-col :span="5">
          <a-card class="category-menu" :bordered="false">
            <template #title>
              <div class="menu-title">文档目录</div>
            </template>
            <a-menu
              :selected-keys="[activeCategory.title]"
              style="width: 100%"
            >
              <a-menu-item
                v-for="category in helpCategories"
                :key="category.title"
                @click="activeCategory = category"
              >
                <template #icon>
                  <icon-font :type="category.icon" />
                </template>
                {{ category.title }}
              </a-menu-item>
            </a-menu>
          </a-card>
        </a-col>

        <a-col :span="19">
          <a-card :bordered="false">
            <template #title>
              <div class="content-title">
                <icon-font :type="activeCategory.icon" />
                <span>{{ activeCategory.title }}</span>
              </div>
            </template>
            <div class="help-sections">
              <div v-for="item in activeCategory.items" :key="item.title" class="help-section">
                <h3>{{ item.title }}</h3>
                <div class="help-content-list">
                  <v-md-editor 
                    :model-value="item.content" 
                    mode="preview"
                  />
                </div>
              </div>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<style scoped>
.help-container {
  padding: 16px 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.help-header {
  text-align: center;
  margin-bottom: 16px;
  background: #fff;
  padding: 16px;
  border-radius: 4px;
}

.help-header h1 {
  font-size: 24px;
  margin-bottom: 8px;
  color: var(--color-text-1);
}

.help-header p {
  color: var(--color-text-3);
  font-size: 13px;
}

.category-menu {
  margin-bottom: 16px;
}

.menu-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text-1);
}

.content-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 500;
}

.help-sections {
  padding: 0 8px;
}

.help-section {
  margin-bottom: 20px;
}

.help-section h3 {
  font-size: 14px;
  margin-bottom: 12px;
  color: var(--color-text-1);
  font-weight: 500;
}

.help-content-list {
  color: var(--color-text-2);
  line-height: 1.6;
  font-size: 13px;
}

.help-content-list p {
  margin-bottom: 6px;
}

:deep(.arco-card) {
  border-radius: 4px;
}

:deep(.arco-card-header) {
  border-bottom: 1px solid var(--color-border);
  padding: 12px 16px;
}

:deep(.arco-menu-item) {
  height: 36px;
  font-size: 13px;
}

:deep(.arco-menu-icon) {
  font-size: 16px;
}

.help-content-list :deep(h3) {
  font-size: 15px;
  font-weight: 500;
  margin: 16px 0 8px;
  color: var(--color-text-1);
}

.help-content-list :deep(ul),
.help-content-list :deep(ol) {
  padding-left: 20px;
  margin: 8px 0;
}

.help-content-list :deep(li) {
  margin-bottom: 4px;
}

.help-content-list :deep(code) {
  background: var(--color-fill-2);
  padding: 2px 4px;
  border-radius: 2px;
  font-size: 12px;
}

.help-content-list :deep(strong) {
  color: var(--color-text-1);
}

.help-content-list :deep(.v-md-editor) {
  border: none;
  background: transparent;
}

.help-content-list :deep(.v-md-editor__preview) {
  padding: 0;
}

.help-content-list :deep(.v-md-editor__preview-wrapper) {
  background: transparent;
}

.help-content-list :deep(.github-markdown-body) {
  background: transparent;
  font-size: 13px;
}

.help-content-list :deep(.github-markdown-body h3) {
  font-size: 15px;
  margin-top: 16px;
  margin-bottom: 8px;
  padding-bottom: 0;
  border-bottom: none;
}

.help-content-list :deep(.github-markdown-body ul),
.help-content-list :deep(.github-markdown-body ol) {
  padding-left: 20px;
  margin: 8px 0;
}

.help-content-list :deep(.github-markdown-body li) {
  margin-bottom: 4px;
}

.help-content-list :deep(.github-markdown-body code) {
  font-size: 12px;
  background: var(--color-fill-2);
  margin: 0 2px;
}

.help-content-list :deep(.github-markdown-body strong) {
  color: var(--color-text-1);
}

.help-content-list :deep(.github-markdown-body p) {
  margin: 8px 0;
  line-height: 1.6;
}
</style> 